CFLAGS = -m32 -ffreestanding -fno-stack-protector -fno-builtin -Wall -Werror -nostdinc -I./header/
LDFLAGS = -melf_i386 -T linker
CFILES = ext2.c fdc.c fs.c fs_dev.c gdt.c idt.c input.c io.c keyboard.c \
main.c memory.c paging.c panic.c print.c rtc.c scheduler.c string.c syscall.c \
elf.c lock.c vga.c environ.c
OBJ = kernel_asm.o isr_asm.o elf_asm.o kernel_c.o \
obj/ext2.c.o obj/fdc.c.o obj/fs.c.o obj/fs_dev.c.o obj/gdt.c.o obj/idt.c.o obj/input.c.o \
obj/io.c.o obj/keyboard.c.o obj/main.c.o obj/memory.c.o obj/paging.c.o obj/panic.c.o \
obj/print.c.o obj/rtc.c.o obj/scheduler.c.o obj/string.c.o obj/syscall.c.o obj/elf.c.o \
obj/lock.c.o obj/vga.c.o obj/environ.c.o
all:
	make pharnos
	make flarno
pharnos:
	nasm -f elf32 -o kernel_asm.o kernel.asm
	nasm -f elf32 -o isr_asm.o header/isr.asm
	nasm -f elf32 -o elf_asm.o header/elf.asm
	for i in $(CFILES); do \
		gcc $(CFLAGS) -o obj/$$i.o -c header/$$i; \
	done
	
	gcc $(CFLAGS) -o kernel_c.o -c kernel.c
	ld $(LDFLAGS) -o kernel.bin $(OBJ) 
floppy: kernel.bin console.elf
	dd if=/dev/zero of=floppy.img bs=1024 count=1440
	/sbin/mke2fs -F floppy.img
	sudo mount -o loop floppy.img floppy/
	cp kernel.bin floppy/
	mkdir floppy/grub
	cp /boot/grub/stage1 floppy/grub
	cp /boot/grub/stage2 floppy/grub
	cp menu.lst floppy/grub/menu.lst
	sudo umount floppy/
	/usr/sbin/grub 
	device (fd0) floppy.img
	root (fd0)
	setup (fd0)
	quit
mount: 
	sudo mount -o loop floppy.img ../floppy/
umount:
	sudo umount ../floppy/
flarno: 
	sudo mount -o loop floppy.img ../floppy/
	cp bin/elf/* ../floppy/
	cp kernel.bin ../floppy/kernel.bin
	sudo umount ../floppy/
clean:
	rm obj/*.o
	rm header/*.?~
	rm *~
	rm kernel.bin
